/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.agilelang.fgl.parser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
import org.agilelang.object.Model;
import org.agilelang.object.Report;
import org.agilelang.object.attributes.ViewReport;

/**
 *
 * @author Roderick
 */
public class ReportProcess extends Process {

    Class report_class;    
    private Class viewreport_class;
    HashMap appReport = new HashMap();

    public ReportProcess() {
        try {
            report_class = Class.forName("org.agilelang.object.Report");
            viewreport_class = Class.forName("org.agilelang.object.attributes.ViewReport");
            appReport = new HashMap();
        } catch (ClassNotFoundException ex) {
            error = "Error  : " + ex.toString();
        }
    }

    public boolean process(String aLine) {
        this.error = "";
        Report report = new Report();
        ViewReport viewreport = new ViewReport();

        String name_report = this.setPluralize(aLine);
        if (this.isReservedWord(name_report.toLowerCase())) {
            this.error = "Error nombre es palabra reservada  : " + name_report;
            return false;
        }
        if (appReport.containsKey(name_report)) {
            report = (Report) appReport.get(name_report);
        } else {
            report = new Report();
            report.name = name_report;
        }
        Function function = Util.getFunction(aLine);

        if (function.name.equals("define")) {
            String[] param = Util.split(function.val, ",");
            for (int i = 0; i < param.length; i++) {
                String[] paramdel = Util.split(param[i], "=");
                String name = paramdel[0];
                String value = paramdel[1].substring(1, paramdel[1].length() - 1);
                if (Util.setField(report_class, name, report, value)) {
                    appReport.put(report.name, report);
                } else {
                    this.error = "Error parametro de la funcion \"define\" no existe : " + name;
                    return false;
                }
            }
        } else if (function.name.equals("addViewReport")) {
            String[] param = Util.split_comas(function.val);
            for (int i = 0; i < param.length; i++) {
                String[] paramdel = Util.split(param[i], "=");
                String name = paramdel[0];
                String value = paramdel[1].substring(1, paramdel[1].length() - 1);
                if (!Util.setField(viewreport_class, name, viewreport, value)) {
                    error = "Error parametro de la funcion \"addView\" no existe : " + name;
                    return false;
                }
            }
            Object arglist[] = new Object[1];
            arglist[0] = viewreport;
            Util.setMethod(viewreport, report_class, report, "addViewReport");
            appReport.put(report.name, report);
        } else {
            this.error = "Error Funcion no existe : " + function.name;
            return false;
        }
        return true;
    }
    
    public List<Report> getListReport() {
        List<Report> listmodel = new ArrayList<Report>();
        SortedSet<String> sortedset = new TreeSet<String>(appReport.keySet());
        Iterator<String> it = sortedset.iterator();
        while (it.hasNext()) {
            Report model = (Report) appReport.get(it.next());
            listmodel.add(model);
        }
        return listmodel;
    }
}
